package de.slothsoft.commons.io;

import java.io.File;
import java.io.FileFilter;

public class ExtensionFileFilter implements FileFilter {

	private boolean directoriesAllowed;
	private String[] extensions;

	public ExtensionFileFilter(String... extensions) {
		this(true, extensions);
	}

	public ExtensionFileFilter(boolean directoriesAllowed, String... extensions) {
		this.extensions = extensions;
		this.directoriesAllowed = directoriesAllowed;
	}

	@Override
	public boolean accept(File file) {
		if (file.isDirectory() && this.directoriesAllowed)
			return true;

		String fileName = file.toString().toLowerCase();

		for (String e : this.extensions) {
			if (fileName.endsWith(e))
				return true;
		}

		return false;
	}

	public boolean isDirectoriesAllowed() {
		return this.directoriesAllowed;
	}

	public void setDirectoriesAllowed(boolean directoriesAllowed) {
		this.directoriesAllowed = directoriesAllowed;
	}

	public String[] getExtensions() {
		return this.extensions;
	}

	public void setExtensions(String[] extensions) {
		this.extensions = extensions;
	}

}
